<template>
  <div>
    <sticky-top>
      <div class="title">
        <span>用户详情</span>
        <span class="back" @click="back"> <i class="iconfont icon-fanhui"></i> 返回 </span>
        <el-divider></el-divider>
      </div>
    </sticky-top>
    <div class="container">
      <div class="wrap">
        <el-row>
          <el-col :lg="16" :md="20" :sm="24" :xs="24">
            <el-form :model="form" status-icon ref="form" label-width="100px" @submit.native.prevent>
              <el-form-item label="头像" prop="avatar">
                <el-input size="medium" v-model="form.avatar" placeholder="请输入头像图片链接"></el-input>
                <img v-if="form.avatar" :src="form.avatar" class="display_img" alt="头像" style="margin-top:8px;" />
              </el-form-item>

              <el-form-item label="角色" prop="role">
                <el-select v-model="form.role" placeholder="请选择角色">
                  <el-option label="普通用户" value="normal" />
                  <el-option label="内部员工" value="staff" />
                  <el-option label="会员用户" value="vip" />
                </el-select>
              </el-form-item>

              <el-form-item label="用户名" prop="nickname">
                <el-input size="medium" v-model="form.nickname" placeholder="请输入用户名"></el-input>
              </el-form-item>

              <el-form-item label="真实姓名" prop="real_name">
                <el-input size="medium" v-model="form.real_name" placeholder="请输入真实姓名"></el-input>
              </el-form-item>

              <el-form-item label="收款码" prop="bank_qr_code">
                <upload-imgs :max-num="bankQrMaxNum" ref="bankQrUpload" :value="bankQrInitData" />
              </el-form-item>

              <el-form-item label="联系方式" prop="mobile">
                <el-input size="medium" v-model="form.mobile" placeholder="请输入联系方式"></el-input>
              </el-form-item>

              <el-form-item>
                <el-button type="primary" @click="submitForm('form')">保存</el-button>
                <el-button @click="resetForm('form')">重置</el-button>
              </el-form-item>
            </el-form>
          </el-col>
        </el-row>
      </div>
    </div>
  </div>
</template>

<script>
import ThirdUser from '@/model/third-user'
import StickyTop from '@/component/base/sticky-top/sticky-top'
import UploadImgs from '@/component/base/upload-image'

export default {
  components: { StickyTop, UploadImgs },
  props: {
    userId: {
      type: String,
      default: null,
    },
  },
  data() {
    return {
      form: {},
      bankQrInitData: [],
      bankQrMaxNum: 1,
    }
  },
  async created() {
    this.$nextTick(async () => {
      await this.getThirdUser()
      // 设置收款码初始图片
      if (this.form.bank_qr_code) {
        this.bankQrInitData = [{ id: 'bank_qr_code', display: this.form.bank_qr_code }]
      } else {
        this.bankQrInitData = []
      }
    })
  },
  methods: {
    async getThirdUser() {
      const res = await ThirdUser.getThirdUser(this.userId)
      this.form = res
    },
    async submitForm(formName) {
      // 获取上传图片
      const imgs = await this.$refs.bankQrUpload.getValue()
      this.form.bank_qr_code = imgs && imgs.length > 0 ? imgs[0].display : ''
      const res = await ThirdUser.editThirdUser(this.userId, this.form)
      if (res.code < window.MAX_SUCCESS_CODE) {
        this.$message.success('保存成功')
        this.getThirdUser()
      } else {
        this.$message.error(res.message || '保存失败')
      }
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    back() {
      this.$emit('editClose')
    },
    errorHandler() {
      return true
    },
    previewImage(url) {
      this.$alert(`<img src='${url}' style='width:100%' />`, '收款码', {
        dangerouslyUseHTMLString: true,
        center: true
      })
    },
    roleText(role) {
      switch (role) {
        case 'normal':
        case 1:
          return '普通用户'
        case 'staff':
        case 2:
          return '内部员工'
        case 'vip':
        case 3:
          return '会员用户'
        default:
          return role || '-'
      }
    },
  },
}
</script>

<style lang="scss" scoped>
.el-divider--horizontal {
  margin: 0;
}

.container {
  .title {
    height: 59px;
    line-height: 59px;
    color: $parent-title-color;
    font-size: 16px;
    font-weight: 500;
    text-indent: 40px;

    .back {
      float: right;
      margin-right: 40px;
      cursor: pointer;
    }
  }

  .wrap {
    padding: 20px;
  }

  .submit {
    float: left;
  }
}

.display_img {
  max-width: 100px;
  max-height: 50px;
  width: auto;
  height: auto;
}

.tree-view-item-key {
  color: red;
}
</style>
